package com.gobygang.client;

/*
 * 
 *
 *
 */

import com.gobygang.client.facebook.FacebookApi;
import com.gobygang.client.facebook.FacebookFactory;
import com.gobygang.client.uibinder.Welcome;
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.core.client.RunAsyncCallback;
import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.user.client.Command;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.Widget;


public class Gobygang implements EntryPoint {

	public static boolean testing = false;
	
	private final static String TAXIGANGLOCALE = "TAXIGANGLOCALE";
	private final static String CRON = "CRON";
	public static DateTimeFormat formatter = DateTimeFormat.getFormat("EEE, MMM d, hh:mm a");


	public Gobygang(){
	}


	public void onModuleLoad() {
//		String localeTest = Window.Location.getParameter("locale");
//		System.out.println("locale="+localeTest);
//		//Log.info("getLocale="+localeTest);
		
		final String agent = Window.Location.getParameter("agent");
		final String tg = Window.Location.getParameter("tg");
		final String pt = Window.Location.getParameter("pt");
		final String rt = Window.Location.getParameter("rt");
		final String cancel = Window.Location.getParameter("cancel");
		
		final String mode = Window.Location.getParameter("mode");
		final String cron = Window.Location.getParameter(CRON);
		boolean localMode = GWT.getHostPageBaseURL().contains("http://127.0.0.1:8888/");
		boolean isFB = "FB".equals(Window.Location.getParameter("userAgent"));
		boolean tooNarrow = Window.getClientWidth() < 989;

		if ("GOTO".equals(mode)){
			String href = Window.Location.getHref();
			href = href.replace("mode=GOTO&", "");
			breakOut1(href);
			Window.Location.assign("http://www.taxigang.com"); 
		}

		if ("FB_PAGE".equals(mode)){
			final String pageId = Window.Location.getParameter("page");
			String likedStr = Window.Location.getParameter("liked");
			final boolean  liked = "true".equals(likedStr);
			//Log.info("welcome: page:"+pageId);
			//Log.info("welcome: liked:"+likedStr);
			////Log.info("welcome: locale "+Window.Location.getParameter("locale"));
			final  Welcome welcome = new Welcome(pageId,liked);
			Command command = new Command(){

				@Override
				public void execute() {
					FacebookApi.initFacebookAPIGobygang();
					RootPanel.get().add(welcome);

				}
				
			};
			hideLoading();
			FacebookFactory.createFBConnection(null, command);
			return;
		}
		
//		if ("FB_AMBASSADOR".equals(mode)){
//			final String pageId = Window.Location.getParameter("page");
//			String likedStr = Window.Location.getParameter("liked");
//			final boolean  liked = "true".equals(likedStr);
//			//Log.info("welcome: page:"+pageId);
//			//Log.info("welcome: liked:"+likedStr);
//			//Log.info("welcome: locale "+Window.Location.getParameter("locale"));
//			final  Ambassador welcome = new Ambassador(pageId,liked);
//			Command command = new Command(){
//
//				@Override
//				public void execute() {
//					FacebookApi.initFacebookAPITaxiGang();
//					RootPanel.get().add(welcome);
//
//				}
//				
//			};
//			TaxiGangMain.hideLoading();
//			FacebookFactory.createFBConnection(null, command);
//			return;
//		}
		
//		if (!localMode && getLocale())
//			return;
		
		final String locale = Window.Location.getParameter("locale");
		final String country = Window.Location.getParameter("co");
		System.out.println(locale);

		GWT.runAsync(new RunAsyncCallback() {
			public void onFailure(Throwable caught) {
				Window.alert("Code download failed");
			}

			public void onSuccess() {
				new GobygangProcessor().onModuleLoad(locale,country,agent,cron);
			}
		});

	}

	public static void hideLoading(){
		//Widget loaderimage = RootPanel.get("loaderimage");
		Widget loaderimage = RootPanel.get("loading");
		loaderimage.setVisible(false);
	}
	
	public native void breakOut1(String href) /*-{
    if ($wnd.top.location != $wnd.location) {
        $wnd.top.location = href;
    }
	}-*/;
}